<?php

namespace app\api\controller;

use app\admin\model\Category;
use app\admin\model\Video;
use app\common\controller\Api;
use app\admin\model\Course as ModelCourse;

class Course extends Api
{
    protected $noNeedLogin = ['index','detail','cate','play'];
    protected $noNeedRight = '*';

    /*课程分类*/
    public function cate()
    {
        $list=Category::where('type','course')->select();
        $this->success('ok',['list'=>$list]);
    }
    // 课程列表
    public function index()
    {
//        $page_size = input('page_size', 10);
        $cate_id=input('cate_id',19);
        $list=ModelCourse::where('show_switch', '1')
            ->where('category_id',$cate_id)
            ->field('id,title,people,keyword,hours,st_id,createtime')
            ->order('weigh desc,id desc')
            ->select();
        $stList=[];
        foreach($list as $k=>$v){
            $list[$k]['createtime']=date('Y-m-d',$v['createtime']);
            if($v['st_id']){
                $stIds=explode('-',$v['st_id']);
                if(count($stIds)>1){
                    $stList[]=Video::where('id',$stIds[1])->field('id,title,safe_link')->find();
                }
            }
        }
        $this->success('ok', ['list'=>$list,'stList'=>$stList]);
    }
    // 课程详情
    public function detail()
    {
        $id = input('a', 0);
        $detail = ModelCourse::get($id);
        if (!$detail) $this->error('课程不存在');
        $this->success('ok', $detail);
    }   
    // 课程章节
    public function play(){
        $id = input('a', 0);
        $user_id=$this->auth->id;
        $packages = ModelCourse::playTree($id,$user_id);
        if (!$packages) $this->error('课程不存在');
        $this->success('ok', $packages);
    }

    public function video()
    {
        $video_id=input('video_id',0);
        $video=Video::find($video_id);
        if (!$video) $this->error('视频不存在');
        $this->success('ok', $video);
    }
    public function myCourse(){
        $user_id=$this->auth->id;
        if(!$user_id) $this->error('用户不存在');
        $list=ModelCourse::myCourse($user_id);
        $this->success('ok', $list);
    }
    

}
